#include "CommonUiUtil.h"
#include "common/PIOStreamOperator.h"
#include "cocostudio/CocoStudio.h"
#include "ui/UIHelper.h"
#include "ui/UIText.h"

CCommonUiUtil::CCommonUiUtil(void)
{
}


CCommonUiUtil::~CCommonUiUtil(void)
{
}

cocos2d::ui::Widget* CCommonUiUtil::initItemPersonLook(VecNode_t& nodes, int32_t index, AnimalLookInfo* info) {
	cocos2d::Node* tNode = cacheListNode(nodes, index, "PersonLookItem.csb");
	cocos2d::ui::Widget* tWidget = ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)tNode, 107);

	std::string tStr;
	ui::Text* tUid = (ui::Text*)ui::Helper::seekWidgetByTag(tWidget, 50);
	tStr.clear();
	tStr << info->m_Uid;
	tUid->setString(tStr);

	ui::Text* tType = (ui::Text*)ui::Helper::seekWidgetByTag(tWidget, 51);
	tStr.clear();
	tStr << info->m_Type;
	tType->setString(tStr);

	ui::Text* tLevel = (ui::Text*)ui::Helper::seekWidgetByTag(tWidget, 52);
	tStr.clear();
	tStr << info->m_Lev;
	tLevel->setString(tStr);

	ui::Text* tName = (ui::Text*)ui::Helper::seekWidgetByTag(tWidget, 53);
	tName->setString(info->m_Name);

	tWidget->setUserData(info);

	return tWidget;
}

cocos2d::ui::Widget* CCommonUiUtil::initItemSkill(VecNode_t& nodes, int32_t index, SkillInfo* info) {
	cocos2d::Node* tNode = cacheListNode(nodes, index, "Item1.csb");
	cocos2d::ui::Widget* tWidget = ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)tNode, 30);
	
	if (info->m_Type != 0) {
		std::string tStr;

		tStr.clear();
		tStr << info->m_Level;
		ui::Text* tNumber = (ui::Text*)ui::Helper::seekWidgetByTag(tWidget, 33);
		tNumber->setString(tStr);

		tWidget->setUserData(info);
	}

	return tWidget;
}

cocos2d::Node* CCommonUiUtil::cacheListNode(VecNode_t& nodes, int32_t index, const char* resource) {
	cocos2d::Node* tNode = nullptr;
	if (index >= nodes.size()) {
		tNode = CSLoader::createNode(resource);
		tNode->retain();
		nodes.push_back(tNode);
	} else {
		tNode = nodes[index];
	}

	return tNode;
}

void CCommonUiUtil::cacheListNodeFree(VecNode_t& nodes) {
	for (size_t i = 0; i != nodes.size(); ++i) {
		cocos2d::Node* tNode = nodes[i];
		tNode->retain();
	}
}
